#include <windows.h>		// Header File For Windows

//#include <gl\glaux.h>		// Header File For The Glaux Library

#include "WindowsWindow.h"
#include "OpenGLRenderer.h"


int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG				msg;									// Windows Message Structure
	BOOL			done=FALSE;								// Bool Variable To Exit Loop
	const int		windowWidth = 640;
	const int		windowHeight = 480;
	bool			fullscreen = true;

	// Ask The User Which Screen Mode They Prefer
	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullscreen=FALSE;							// Windowed Mode
	}


	WindowInterface* window = new WindowsWindow();
	RendererInterface* renderer =  new OpenGLRenderer( );

	if( !window->initialise( windowWidth, windowHeight, WindowInterface::WindowColourDepthEnumeration_32, fullscreen ) || 
		!renderer->initialise( windowWidth, windowHeight ) )				
	{
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return 0;	
	}


	while(!done)									// Loop That Runs While done=FALSE
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			
			if ( window->isKeyPressed( VK_ESCAPE ) )				// Was ESC Pressed?
			{
				done=TRUE;						// ESC Signalled A Quit
			}
			else								// Not Time To Quit, Update Screen
			{
				renderer->renderScene();					// Draw The Scene
				window->redraw();							// Make the window redraw
			}
		}
	}

	renderer->shutdown( );
	delete renderer;

	window->shutdown( );
	delete window;

	return (msg.wParam);							// Exit The Program
}
